﻿using Assets.Script.Config;
using Player.Weapons.Wands;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;

namespace Player.Weapons
{
    public class AttackWay:IDisposable
    {
        protected bool inAttack = false;
        protected Weapon weapon;
        /// <summary>
        /// 子弹实体，在初始化时自动加载
        /// </summary>
        protected GameObject bullet;
        protected bool canAttack=true;
        /// <summary>
        /// 能否多弹道
        /// </summary>
        protected bool canMultiple = true;

        public AttackWay(Weapon weapon,string bulletName)
        {
            this.weapon = weapon;
            bullet = Resources.Load<GameObject>(ResourcePath.Bullet+ weapon.WeaponName + "/Bullet/"+ bulletName);
        }

        public void AttackStart()
        {
            inAttack = true;
            AttackStart_m();
        }

        public void AttackEnd()
        {
            inAttack = false;
            AttackEnd_m();
        }

        protected void Attack_m()
        {
            if (canAttack)
            {
                Attack();
                new Timer(1f/weapon.playerBody.PlayerData.AtkSpeed, false, weapon.playerBody.PlayerData.TimeScale, () => {
                    canAttack = true;
                    if (inAttack)
                    {
                        Attack_m();
                    }
                }).Start();
                canAttack = false;
            }
        }

        /// <summary>
        /// 每帧其他动作
        /// </summary>
        public virtual void Update_Other()
        {

        }

        /// <summary>
        ///  攻击开始动作
        /// </summary>
        protected virtual void AttackStart_m()
        {

        }

        /// <summary>
        /// 攻击结束动作
        /// </summary>
        protected virtual void AttackEnd_m()
        {

        }

        /// <summary>
        /// 攻击动作，制作子弹,控制弹道等
        /// </summary>
        protected virtual void Attack()
        {
            if (weapon.playerBody.PlayerData.AttackDir.forward>0)
                CreateBulletWithDirAndNum(weapon.playerBody.fire_forward, ref weapon.playerBody.PlayerData.AttackDir.forward);
            if (weapon.playerBody.PlayerData.AttackDir.back > 0)
                CreateBulletWithDirAndNum(weapon.playerBody.fire_back, ref weapon.playerBody.PlayerData.AttackDir.back);
            if (weapon.playerBody.PlayerData.AttackDir.left > 0)
                CreateBulletWithDirAndNum(weapon.playerBody.fire_left, ref weapon.playerBody.PlayerData.AttackDir.left);
            if (weapon.playerBody.PlayerData.AttackDir.rigt > 0)
                CreateBulletWithDirAndNum(weapon.playerBody.fire_right, ref weapon.playerBody.PlayerData.AttackDir.rigt);
        }

        /// <summary>
        /// 制作子弹
        /// </summary>
        /// <returns></returns>
        protected virtual GameObject CreateBullet(Transform orgin)
        {
            var clo = GameObject.Instantiate(bullet);
            clo.GetComponent<PlayerBulletBodyBase>().Init(weapon.cores,weapon.playerBody.PlayerData.BulletData,weapon.playerBody.PlayerData.TimeScale,weapon.BulletEffectLevel);
            clo.transform.rotation = orgin.rotation;
            clo.transform.position = orgin.position;
            clo.transform.localScale = weapon.playerBody.PlayerData.BulletData.BulletScale * Vector3.one;
            return clo;
        }

        protected virtual void CreateBulletWithDirAndNum(Transform orgin,ref int num)
        {
            if (canMultiple)
            {
                if (num == 1)
                {
                    var clo1 = CreateBullet(orgin);
                }
                else if (num == 2)
                {
                    //并排发射处理
                    var clo1 = CreateBullet(orgin);
                    clo1.transform.position = new Vector3(clo1.transform.position.x , clo1.transform.position.y - 0.3f, clo1.transform.position.z);
                    var clo2 = CreateBullet(orgin);
                    clo2.transform.position = new Vector3(clo2.transform.position.x , clo2.transform.position.y + 0.3f, clo2.transform.position.z);
                }
                else
                {
                    //均匀±15°发射处理
                    var perAngle = 30f / (float)(num - 1f);
                    for (int i = 0; i < num; i++)
                    {
                        var clo = CreateBullet(orgin);
                        clo.transform.Rotate(0, 0, -15f + i * perAngle);
                    }
                }
            }
            else
            {
                var clo1 = CreateBullet(orgin);
            }
        }

        #region 手动实现回收机制
        bool disposed = false;

        public void Dispose()
        {
            //必须为true
            Dispose(true);
            //通知垃圾回收机制不再调用终结器（析构器）
            GC.SuppressFinalize(this);
        }

        /// <summary>
        /// 非密封类修饰用protected virtual
        /// 密封类修饰用private
        /// </summary>
        /// <param name="disposing"></param>
        protected virtual void Dispose(bool disposing)
        {
            if (disposed)
            {
                return;
            }
            if (disposing)
            {
                if (weapon!=null)
                {
                    weapon.Dispose();
                }
            }
            if (bullet!=null)
            {
                bullet = null;
            }
            //让类型知道自己已经被释放
            disposed = true;
        }

        ~AttackWay()
        {
            Dispose(false);
        }
        #endregion
    }
}
